﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AJRavindiran.Jolt.RuneScape.Models.Characters;

namespace AJRavindiran.Jolt.RuneScape.Network.Packets.Builders
{
    /// <summary>
    /// Represents character related update masks.
    /// </summary>
    public class CharacterUpdateMasks
    {
        #region Methods
        /// <summary>
        /// Appends the update masks.
        /// </summary>
        protected void AppendUpdateBlock(Character character, bool forceAppearance)
        {
            int mask = 0x0;

            if (character.GetUpdateFlags().FaceToUpdateRequired)
                mask |= 0x20;
            if (character.GetUpdateFlags().GraphicsUpdateRequired)
                mask |= 0x400;
            if (character.GetUpdateFlags().ChatTextUpdateRequired)
                mask |= 0x8;
            if (character.GetUpdateFlags().GraphicsUpdateRequired)
                mask |= 0x1;
            if (character.GetUpdateFlags().AppearanceUpdateRequired || forceAppearance)
                mask |= 0x80;

            if (mask >= 0x100)
            {
                mask |= 0x10;
                character.GetStreamWriter().WriteByte(mask & 0xFF).WriteByte(mask >> 8);
            }
            else
            {
                character.GetStreamWriter().WriteByte(mask & 0xFF);
            }

            if (character.GetUpdateFlags().FaceToUpdateRequired)
                this.AppendFaceToUpdate(character);
            if (character.GetUpdateFlags().GraphicsUpdateRequired)
                this.AppendGraphicsUpdate(character);
            if (character.GetUpdateFlags().ChatTextUpdateRequired)
                this.AppendChatTextUpdate(character);
            if (character.GetUpdateFlags().AnimationUpdateRequired)
                this.AppendAnimationUpdate(character);
        }

        private void AppendFaceToUpdate(Character character)
        {
            character.GetStreamWriter().WriteShort(character.GetUpdateFlags().FaceTo);
        }

        private void AppendGraphicsUpdate(Character character)
        {
            character.GetStreamWriter().WriteShort(character.GetLastGraphics().Id);
            character.GetStreamWriter().WriteIntV1(character.GetLastGraphics().Delay);
        }

        private void AppendAnimationUpdate(Character character)
        {
            character.GetStreamWriter().WriteShort(character.GetLastAnimation().Id);
            character.GetStreamWriter().WriteByteS(character.GetLastAnimation().Delay);
        }

        private void AppendChatTextUpdate(Character character)
        {
            byte[] chatStr = new byte[256];
            character.GetStreamWriter().WriteShortA(character.GetLastChatMessage().Effects);
            character.GetStreamWriter().WriteByteC((int) character.GetDetails().Rights);
            chatStr[0] = (byte)character.GetLastChatMessage().Text.Length;
            int offset = 1 + Utilities.ChatUtilities.EncryptChat(
                chatStr, 0, 1, character.GetLastChatMessage().Text.Length, 
                new ASCIIEncoding().GetBytes(character.GetLastChatMessage().Text));
            character.GetStreamWriter().WriteByteC(offset);
            character.GetStreamWriter().WriteBytes(chatStr, 0, offset);
        }
        #endregion Methods
    }
}
